//
//  NewAddCommonTeachViewController.m
//  fitnessmanage
//
//  Created by wujian on 2018/1/17.
//  Copyright © 2018年 zongquankeji. All rights reserved.
//

#import "NewAddCommonTeachViewController.h"
#import "AddCommonTeachView.h"
#import "SelectWorkViewController.h"
//#import "VenuePlaceViewController.h"
#import "MOFSPickerManager.h"
#import "HDAlertView.h"
#import "TeachClassViewController.h"
#import "EBDate.h"

@interface NewAddCommonTeachViewController ()

@property(nonatomic,weak)AddCommonTeachView *addCommonTeachView;

@property(nonatomic,strong)NSMutableDictionary *dataDict;

@end

@implementation NewAddCommonTeachViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    self.title = @"新增团课排期";
    
    self.view.backgroundColor = BackGroundColor;
    
    [self createSubView];
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(sureBtnClick)];
    
    [rightButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:CSBlackColor,NSFontAttributeName:MidBigFont} forState:UIControlStateNormal];
    
    [rightButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:CSBlackColor,NSFontAttributeName:MidBigFont} forState:UIControlStateHighlighted];
    
    self.navigationItem.rightBarButtonItem = rightButtonItem;
}


-(void)createSubView{
    
    AddCommonTeachView *addCommonTeachView = [[AddCommonTeachView alloc] initWithFrame:self.view.bounds];
    
    [addCommonTeachView.classImageView sd_setImageWithURL:[NSURL URLWithString:self.courseModel.Pic] placeholderImage:PlaceHold];
    
    addCommonTeachView.classLabelName.text = self.courseModel.Name;
    
    addCommonTeachView.timerLabel.text = [NSString stringWithFormat:@"%@min",self.courseModel.Times];
    
    [self.view addSubview:addCommonTeachView];
    
    self.addCommonTeachView = addCommonTeachView;
    
    [addCommonTeachView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.mas_equalTo(self.view);
    }];
    
    //选择教练
    [addCommonTeachView.teachBtnView.choseBtn addTarget:self action:@selector(choseCoaches)];
    
    //选择场地
    [addCommonTeachView.teachPlaceView.choseBtn addTarget:self action:@selector(chosePlace)];
    
    //选择时间
    [addCommonTeachView.courseTimeView.choseBtn addTarget:self action:@selector(courseTimeClick)];
    
    //预约开发
    [addCommonTeachView.openView.choseBtn addTarget:self action:@selector(pointBtnClick)];
    
    
    //先设置课程ID
    [self.dataDict setObject:[NSString stringWithFormat:@"%@",self.courseModel.ID] forKey:@"CourseID"];
    
    
}


/**
 选择教练
 */
-(void)choseCoaches{
    
    __weak typeof(self)weakSelf = self;
    
    SelectWorkViewController *selectWorkVC = [[SelectWorkViewController alloc] init];
    
    selectWorkVC.from = @"教练";
    
    selectWorkVC.sendSelectWorkerBlock = ^(NSString *workId, NSString *workName) {
        
        weakSelf.addCommonTeachView.teachBtnView.choseLabel.text = workName;
        
        weakSelf.addCommonTeachView.teachBtnView.choseLabel.textColor = TFColor;
        
        [weakSelf.dataDict setObject:[NSString stringWithFormat:@"%@",workId] forKey:@"TeacherID"];
    };
    
    [self.navigationController pushViewController:selectWorkVC animated:YES];
}


/**
 选择场地
 */
-(void)chosePlace{
    
//    __weak typeof(self)weakSelf = self;
//    
//    VenuePlaceViewController *venuePlaceVC = [[VenuePlaceViewController alloc] init];
//    
//    venuePlaceVC.sendSelectVenuePlaceBlock = ^(NSString *placeId, NSString *placeName){
//        
//        weakSelf.addCommonTeachView.teachPlaceView.choseLabel.text = placeName;
//        
//        weakSelf.addCommonTeachView.teachPlaceView.choseLabel.textColor = TFColor;
//        
//        [weakSelf.dataDict setObject:[NSString stringWithFormat:@"%@",placeId] forKey:@"ShopPlaceID"];
//    };
//    
//    [self.navigationController pushViewController:venuePlaceVC animated:YES];
}


/**
 选择时间
 */
-(void)courseTimeClick{
    
    __weak typeof(self)weakSelf = self;
    
    [[MOFSPickerManager shareManger] showDatePickerWithTag:1220 title:@"选择时间" cancelTitle:@"取消" commitTitle:@"确定" datePickerMode:UIDatePickerModeCountDownTimer commitBlock:^(NSDate *date) {
        
        NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//创建一个日期格式化器
        
        dateFormatter.dateFormat=@"HH小时mm分";//指定转date得日期格式化形式
        
        weakSelf.addCommonTeachView.courseTimeView.choseLabel.text = [dateFormatter stringFromDate:date];
        
        weakSelf.addCommonTeachView.courseTimeView.choseLabel.textColor = TFColor;
        
        [weakSelf.dataDict setObject:[dateFormatter stringFromDate:date] forKey:@"Times"];
        
    } cancelBlock:^{
        
        
    }];
}


#pragma mark 预约开发
-(void)pointBtnClick{
    
    __weak typeof(self)weakSelf = self;
    
    HDAlertView *sheetView = [HDAlertView showActionSheetWithTitle:nil];
    
    sheetView.buttonsListStyle = HDAlertViewButtonsListStyleRows;
    
      
    [sheetView addButtonWithTitle:@"立即开放" type:HDAlertViewButtonTypeDefault handler:^(HDAlertView *alertView) {
        
        weakSelf.addCommonTeachView.openView.choseLabel.text = @"立即开放";
        
        weakSelf.addCommonTeachView.openView.choseLabel.textColor = TFColor;
        
        [weakSelf.dataDict setObject:@"1" forKey:@"OrderType"];
        
        [weakSelf.dataDict removeObjectForKey:@"OrderHour"];
    }];
    
    [sheetView addButtonWithTitle:@"定时开放" type:HDAlertViewButtonTypeDefault handler:^(HDAlertView *alertView) {
        
        [weakSelf.dataDict setObject:@"2" forKey:@"OrderType"];
        
        [weakSelf.dataDict removeObjectForKey:@"OrderHour"];
        
        [weakSelf timingTime];
    }];

    [sheetView addButtonWithTitle:@"课程前3小时开放" type:HDAlertViewButtonTypeDefault handler:^(HDAlertView *alertView) {
        
        weakSelf.addCommonTeachView.openView.choseLabel.text = @"前3小时开放";
        
        weakSelf.addCommonTeachView.openView.choseLabel.textColor = TFColor;
        
        [weakSelf.dataDict setObject:@"3" forKey:@"OrderType"];
        
        [weakSelf.dataDict setObject:@"3" forKey:@"OrderHour"];
    }];
    
    [sheetView addButtonWithTitle:@"确定" type:HDAlertViewButtonTypeDefault handler:^(HDAlertView *alertView) {
        
        
    }];
    
    [sheetView show];    
}



/**
 定时开发选择时间
 */
-(void)timingTime{
    
    __weak typeof(self)weakSelf = self;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [[MOFSPickerManager shareManger] showDatePickerWithTag:1219 title:@"选择时间" cancelTitle:@"取消" commitTitle:@"确定" datePickerMode:UIDatePickerModeDateAndTime commitBlock:^(NSDate *date) {
            
            NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//创建一个日期格式化器
            
            dateFormatter.dateFormat=@"yyyy-MM-dd HH:mm:ss";//指定转date得日期格式化形式
            
            weakSelf.addCommonTeachView.openView.choseLabel.text = [dateFormatter stringFromDate:date];
            
            weakSelf.addCommonTeachView.openView.choseLabel.textColor = TFColor;
            
            [weakSelf.dataDict setObject:[dateFormatter stringFromDate:date] forKey:@"OrderDateTime"];
            
        } cancelBlock:^{
            
            
        }];
        
    });
    
}


/**
 确定按钮点击
 */
-(void)sureBtnClick{
    
    //先把排课的时间处理一下
    NSMutableArray *items = [[NSMutableArray alloc] init];
    
    NSString *times = [self.dataDict objectForKey:@"Times"];
    
    if ([[self.dataDict objectForKey:@"ShopPlaceID"] isEmptyString] ||
        [[self.dataDict objectForKey:@"TeacherID"] isEmptyString] ||
        [[self.dataDict objectForKey:@"OrderType"] isEmptyString]) {
        
        [MBProgressHUD showText:@"请输入必填项"];
        
        return;
    }
    
    //单节可约人数
    NSString *Persons = self.addCommonTeachView.cardCourseView.titleTF.text;
    
    //单节可约人数
    NSString *MinPersons = self.addCommonTeachView.minCourseView.titleTF.text;
    
    if ([Persons isEmptyString] || [MinPersons isEmptyString]) {
        
        [MBProgressHUD showText:@"请输入必填项"];
        
        return;
    }
    
    if (!times) {
        
        [MBProgressHUD showText:@"请选择课程时间"];
        
        return;
    }
    
    NSRange range1 = NSMakeRange(0, 2);
    int hour = [[times substringWithRange:range1] intValue];
    
    NSRange range = NSMakeRange(4, 2);
    
    int min = [[times substringWithRange:range] intValue];
    
    
    //这里如果有排课时间的话，肯定只能有一个Key
    NSString *key = [[self.teachDict allKeys] firstObject];
    
    NSArray *timeArray = [self.teachDict objectForKey:key];
    
    for (NSString *timeStr in timeArray) {
        
        NSMutableDictionary *itemDict = [[NSMutableDictionary alloc] init];
        
        NSString *BeginDateTime = [NSString stringWithFormat:@"%@ %@",timeStr,key];
        
        NSString *EndDateTime = [BeginDateTime addHourWith:hour andMin:min];
        
        [itemDict setObject:BeginDateTime forKey:@"BeginDateTime"];
        
        [itemDict setObject:EndDateTime forKey:@"EndDateTime"];
        
        [items addObject:itemDict];
    }
    
    [self.dataDict setObject:items forKey:@"Items"];
    
    [self.dataDict setObject:Persons forKey:@"Persons"];
    
    [self.dataDict setObject:MinPersons forKey:@"MinPersons"];
    
    if ([self.type isEqualToString:@"mord"]) {
     
        [self.dataDict setObject:@"0" forKey:@"Type"];
    }
    else{
        
        [self.dataDict setObject:@"1" forKey:@"Type"];
    }
    
    VenueModel *venueModel = [VenueModel venue];
    
    [self.dataDict setObject:venueModel.venueId forKey:@"ShopID"];
    
    NSArray *yearArray = [EBDate getFirstAndLastDayOfThisYear];
//
    [self.dataDict setObject:[yearArray firstObject] forKey:@"BeginDate"];
//
    [self.dataDict setObject:[yearArray lastObject] forKey:@"EndDate"];
    
    [MBProgressHUD showMessage:@"上传中..." toView:self.view];
        
    [HTTPTool POSTRaw:Post_CoursePlanOperate parameters:self.dataDict success:^(id responseObject) {
        
        [MBProgressHUD hideHUDForView:self.view];
        
        if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0){
            
            [MBProgressHUD showText:@"操作成功"];
            
            for (UIViewController *temp in self.navigationController.viewControllers) {
                
                if ([temp isKindOfClass:[TeachClassViewController class]]) {
                    
                    [self.navigationController popToViewController:temp animated:YES];
                    
                    [NotificationCenter postNotificationName:@"ReloadWeekDay" object:nil userInfo:nil];
                }
            }
            
        }
        else{
            
            [MBProgressHUD showText:[responseObject objectForKey:@"Message"]];
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUDForView:self.view];
        
        [MBProgressHUD showText:@"创建失败"];
    }];
}


-(NSMutableDictionary *)dataDict{
    
    if (!_dataDict) {
        
        _dataDict = [[NSMutableDictionary alloc] init];
    }
    
    return _dataDict;
}

@end
